
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import status,serializers

class MultipleDestoryMixin:


    def multiple_destory(self,request,*args,**kwargs):
        delete_ids=request.data.get('ids')
        if not delete_ids:
            return Response(data={'detail':'缺少必传参数'},status=status.HTTP_400_BAD_REQUEST)

        if not isinstance(delete_ids,list):
            return Response(data={'detail':'参数格式错误'},status=status.HTTP_400_BAD_REQUEST)


        queryset=self.get_queryset()
        query_ids=queryset.filter(id__in=delete_ids)
        if len(delete_ids)!=query_ids.count():
            return Response(data={'detail':'删除数据不存在'},status=status.HTTP_400_BAD_REQUEST)

        query_ids.delete()

        return Response(data={'detail':'成功删除数据'},status=status.HTTP_200_OK)

class MultipleOpenMixin:



    def multiple_destory(self,request,*args,**kwargs):
        delete_ids=request.data.get('ids')
        is_open=request.data.get('is_open','0')
        if not delete_ids:
            return Response(data={'detail':'缺少必传参数'},status=status.HTTP_400_BAD_REQUEST)

        if not isinstance(delete_ids,list):
            return Response(data={'detail':'参数格式错误'},status=status.HTTP_400_BAD_REQUEST)


        queryset=self.get_queryset()
        query_ids=queryset.filter(id__in=delete_ids)
        if len(delete_ids)!=query_ids.count():
            return Response(data={'detail':'删除数据不存在'},status=status.HTTP_400_BAD_REQUEST)

        query_ids.update(delete_flag=is_open)

        return Response(data={'detail':'成功修改数据'},status=status.HTTP_200_OK)

